/**
 * The DialogTag user-interface tag conversor class
 * This component have two classes to handle it, according to the new syslib best practices. 
 * The first class create the component itself, and can be called and handled by javascript code instead of tags only.
 * The second class (with the suffic Tag) convert the tag into the component.
 * @class syslib.ui.DialogTag
 * @author Gabriel Ricci
 */
syslib.ui.DialogTag = __class(syslib.ui.VisualTag, 
/** 
 * @lends syslib.ui.DialogTag
 */
{
	dialog : null,
	options : {
		target : false
	}, 
	
	prepare : function(){
		// set the title
		this.options.title = this.__attributes.title;
		
		// set the content
		this.options.content = this.__innerHTML;
		
		
		return true;
	},

	execute : function(){
		// create the dialog
		this.dialog = new syslib.ui.Dialog(this.options);
		
		// return the dialog element
		return this.dialog.getElement();
	},

	finalize : function(){
		return true;	
	}, 
	
	onEnterBody : function(){
		setTimeout(function(){
			this.dialog.show();
		}.applyTo(this), 1000);
		
	}
});

/**
 * The Dialog user-interface component class
 * This component have two classes to handle it, according to the new syslib best practices. 
 * The first class create the component itself, and can be called and handled by javascript code instead of tags only.
 * The second class (with the suffic Tag) convert the tag into the component.
 * @class syslib.ui.Dialog
 * @author Gabriel Ricci
 */
syslib.ui.Dialog = __class();
syslib.ui.Dialog.prototype = {
	options : {
		id : "dialog", 
		title : "Dialog", 
		content : "", 
		target : document.body, 
		enableDrag : true
	}, 
	
	element : null, 
	references : null, 
	
	__construct : function(options){
		// set the options
		syslib.object.extend(this.options, options);
	
		// load the style
		syslib.loadStyleForComponent("dialog");
		
		// create the element
		this.createElement();
		
		// append
		this.append();
	}, 
	
	createElement : function(){
		// create the structure
		var elemtJSON = ["div", {"class" : "dialog_container", "id" : this.options.id},
			["div", {"class" : "dialog_title", "innerName" : "titleBar"},
				["span", {"class" : "dialog_title_text", "innerName" : "title"}, 
					this.options.title
				], 
				["div", {"class" : "dialog_close_button", "innerName" : "close"}]
			],
			["div", {"class" : "dialog_body", "innerName" : "body"}, 
				this.options.content
			]
		];

		// create the object
		var objectData 	= syslib.element.createFromJSON(false, elemtJSON);
		this.element		= objectData.element;
		this.references	= objectData.references;

		// drag and drop
		if(__valid(this.options.enableDrag)){
			new syslib.ui.Drag(this.references.title, this.element, {
				alpha: false
			});
		}
		
		// events
		syslib.event.listen("mouseover", this.references.close, function(){
			this.references.close.className = "dialog_close_button_over";
		}.applyTo(this));
		syslib.event.listen("mouseout", this.references.close, function(){
			this.references.close.className = "dialog_close_button";
		}.applyTo(this));
		syslib.event.listen("click", this.references.close, function(){
			this.dismiss();
		}.applyTo(this));
 	}, 
 	
 	append : function(){
 		if(__valid(this.options.target)){
 			this.options.target.appendChild(this.element);
 		}
 	}, 
 	
 	getElement : function(){
 		return this.element;
 	}, 
 	
 	show : function(){
 		if(syslib.ui.fx.isLoaded("syslib.ui.fx.Move")){
 			syslib.ui.fx.Move.toCenter(this.element);
 		} else{
 			syslib.ui.position.show(this.element);
 			syslib.ui.position.center(this.element);
 		}
 	}, 
 	
 	dismiss : function(){
 		// calculates a random position
 		x = (Math.random() * 1000) * -1;
 		y = (Math.random() * 1000) * -1;
 		if(x >= -500){ x -= 500;}
 		if(y >= -500){ y -= 500;}
 		
 		
 		if(syslib.ui.fx.isLoaded("syslib.ui.fx.Move")){
 			syslib.ui.fx.Move.toPoint(this.element, x, y);
 		} else{
 			syslib.ui.position.hide(this.element);
 		}
 	}
};